/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

/**
 *
 * @author root
 */

import beans.Pessoa;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Query;


public class DaoGenerico<T> {

    private final Class classe;
    private final Session session;
    private Query query;

    public DaoGenerico(Session session, Class classe) {
        this.session = session;
        this.classe = classe;
    }

    public void remover(T c) {
        session.beginTransaction().begin();
        this.session.delete(c);
        this.session.flush();
        session.beginTransaction().commit();
    }

    public void adicionar(T u) {
        this.session.save(u);
    }

    public void atualizar(T c) {
        session.beginTransaction().begin();
        this.session.update(c);
        session.beginTransaction().commit();
    }

    public T procurar(int id, Class c) {
        return (T) this.session.load(c, id);
    }

    public List<T> listarTudo() {
    return this.session.createCriteria(classe).list();
  }


    public Pessoa logar(String email, String senha){
    Pessoa pessoa = new Pessoa();
    String hql = "from Pessoa pessoa where pessoa.email=? and pessoa.senha=?";
    query = this.session.createQuery(hql);
    query.setParameter(0, email);
    query.setParameter(1, senha);
    pessoa = (Pessoa) query.uniqueResult();
    return pessoa;


}
}
